package com.peas.platform.module.icims.base.sys.api;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.peas.platform.common.core.dto.ResponseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(tags = "自定义接口-条形码、二维码接口")
@RestController
@RequestMapping("/api/v1/customCode")
@Slf4j
public class CustomCodeController {

    private final String formatName = "jpg";

    @SneakyThrows
    @ApiOperation(value = "生成条形码")
    @GetMapping(value = "/barcode")
    public ResponseDto<String> barcode(@RequestParam(value = "content") String content,
                                       @RequestParam(value = "width") Integer width,
                                       @RequestParam(value = "height") Integer height) {
        if (width == null)
            width = 300;
        if (height == null)
            height = 100;
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_13, width, height, null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, formatName, out);
        try (InputStream is = new ByteArrayInputStream(out.toByteArray())) {
            return ResponseDto.getSuccessResponseDto(Base64.encode(is));
        }
    }

    @SneakyThrows
    @ApiOperation(value = "生成二维码")
    @GetMapping(value = "/qrcode")
    public ResponseDto<String> qrcode(@RequestParam(value = "content") String content,
                                      @RequestParam(value = "width") Integer width,
                                      @RequestParam(value = "height") Integer height) {
        if (width == null)
            width = 300;
        if (height == null)
            height = 300;
        BufferedImage bi = QrCodeUtil.generate(content, width, height);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bi, formatName, out);
        try (InputStream is = new ByteArrayInputStream(out.toByteArray())) {
            return ResponseDto.getSuccessResponseDto(Base64.encode(is));
        }
    }

    @SneakyThrows
    @ApiOperation(value = "解析二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileType", value = "文件类型(支持类型：word,excel,powerpoint,image,voice,video,deploy)"),
    })
    @PostMapping(value = "/resolveQrcode")
    public ResponseDto<List<String>> upload(@RequestPart(value = "file") MultipartFile file) {
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        HashMap<DecodeHintType, String> hints = new HashMap<>(1);
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Result[] results = new GenericMultipleBarcodeReader(multiFormatReader).decodeMultiple(bitmap, hints);
        List<String> resultList = new ArrayList<>(results.length);
        for (Result result : results) {
            resultList.add(result.getText());
        }
        return ResponseDto.getSuccessResponseDto(resultList);
    }

}
